﻿using System;
using System.Security.Cryptography;
using System.Text;

namespace FrbaCommerce
{
    public class CodigoHash
    {
        public CodigoHash() { }

        public string GetHash(string text)
        {
            string hashString;

            hashString = GetSHA256(text);

            return hashString;
        }

        public bool CheckHash(string original, string hashString)
        {
            string originalHash = GetHash(original);
            return (originalHash == hashString);
        }

        private string GetSHA256(string text)
        {   
            UnicodeEncoding UE = new UnicodeEncoding();
            byte[] hashValue;
            byte[] message = UE.GetBytes(text);

            SHA256Managed hashString = new SHA256Managed();
            string hex = "";

            hashValue = hashString.ComputeHash(message);
            foreach (byte x in hashValue)
            {
                hex += String.Format("{0:x2}", x);
            }
             
            return hex; 
        }

    }
}

